package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVo;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(ArrayList<OrderSetting> orderSettingArrayList) {
        if (orderSettingArrayList != null && orderSettingArrayList.size() > 0) {
            orderSettingDao.add(orderSettingArrayList);
        }

    }

    @Override
    public List<OrderSettingVo> getOrderSettingByMonth(String data) {
        //参数格式为：2019-03拼接成2019-03-01  -  2019-03-31
        String begin = data + "-1";
        String end = data + "-31";
        return orderSettingDao.getOrderSettingByMonth(begin, end);
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        /*根据日期查询数据库当中有没有设置*/
        long count = orderSettingDao.findDate(orderDate);
        if(count>0){
            /*存在更新*/
            orderSettingDao.editNumberByDate(orderSetting);
        }else {
            /*不存在添加*/
        orderSettingDao.addDate(orderSetting);
        }
    }
}
